﻿using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using ThridGroup.ERP.DTO.ProductionSystemDto.ProductionMaterialDto;
using ThridGroup.ERP.ProductionSystem;

namespace ThridGroup.ERP.Web.Controllers.ProductionSystem
{
    [Route("ProductionSystem/ProductionMaterial/[action]")]
    /// <summary>
    /// 生产领料控制器
    /// </summary>
    public class ProductionMaterialController : Controller
    {
        private readonly IProductionMaterialServices _productionMaterialServices;

        public ProductionMaterialController(IProductionMaterialServices productionMaterialServices)
        {
            _productionMaterialServices = productionMaterialServices;
        }

        /// <summary>
        /// 生产领料列表页面
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            return View("~/Views/ProductionSystem/ProductionMaterial/Index.cshtml");
        }

        /// <summary>
        /// 创建生产领料页面
        /// </summary>
        /// <returns></returns>
        public IActionResult Create()
        {
            return View("~/Views/ProductionSystem/ProductionMaterial/Create.cshtml");
        }

        /// <summary>
        /// 编辑生产领料页面
        /// </summary>
        /// <param name="id">生产领料ID</param>
        /// <returns></returns>
        public IActionResult Edit(int id)
        {
            ViewBag.Id = id;
            return View("~/Views/ProductionSystem/ProductionMaterial/Edit.cshtml");
        }

        /// <summary>
        /// 获取生产领料列表
        /// </summary>
        /// <param name="input">查询参数</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetList([FromQuery] GetProductionMaterialDto input)
        {
            var result = await _productionMaterialServices.GetListAsync(input);
            return Json(result);
        }

        /// <summary>
        /// 根据ID获取生产领料详情
        /// </summary>
        /// <param name="id">生产领料ID</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Get(int id)
        {
            var result = await _productionMaterialServices.GetAsync(id);
            return Json(result);
        }

        /// <summary>
        /// 创建生产领料
        /// </summary>
        /// <param name="input">创建参数</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Create([FromBody] CreateProductionMaterialDto input)
        {
            var result = await _productionMaterialServices.CreateAsync(input);
            return Json(result);
        }

        /// <summary>
        /// 更新生产领料
        /// </summary>
        /// <param name="id">生产领料ID</param>
        /// <param name="input">更新参数</param>
        /// <returns></returns>
        [HttpPut]
        public async Task<IActionResult> Update(int id, [FromBody] UpdateProductionMaterialDto input)
        {
            var result = await _productionMaterialServices.UpdateAsync(id, input);
            return Json(result);
        }

        /// <summary>
        /// 删除生产领料
        /// </summary>
        /// <param name="id">生产领料ID</param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<IActionResult> Delete(int id)
        {
            var result = await _productionMaterialServices.DeleteAsync(id);
            return Json(result);
        }

        /// <summary>
        /// 生成领料单编号
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GenerateCode()
        {
            var code = await _productionMaterialServices.GenerateIssuanceOrderCodeAsync();
            return Json(new { code = 0, data = code });
        }
    }
}
